// pages/photo/photo.js
import weapp from '../../components/weapp/index';

var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
var base = require('../../assets/js/base.js')

var base64 = require('../../assets/js/base64.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    token: '',
    randomKey: '',
    sign: '',
    userId: '',

    equArr:[],            //附近设备列表
    equ: '',  //用户选择的设备
    icons_equ: base64.camera_eqe,
    icons_camera: base64.camera,

    tempFilePaths: '',
  },

  chooseimage: function () {
    var that = this;
    var PED = wx.getStorageSync('PED')
    if (!PED && that.data.equ) {
      wx.setStorageSync('PED', that.data.equ.id)  //通过页面跳转再次将设备ID设置到缓存中
    }
    wx.navigateTo({
      url: '/pages/camera/camera',
    })
  },
  //根据当前经纬度搜索附近的设备
  searchEqe: function (latitude, longitude){
    var that = this;
    var simpleObject = {
      latitude: latitude,
      longitude: longitude
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/equipment/getByGps';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        that.setData({
          equArr: res.data,
          hiddenmodalput: !that.data.hiddenmodalput
          });
      })
  },
  // 定位设备位置
  chLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(latitude)
        console.log(longitude)
        console.log(accuracy)
        console.log(speed)

        that.searchEqe(latitude, longitude)
      }
    })
  },
  //选择设备
  equClick: function(e){
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    that.setData({
      equ: that.data.equArr[index],
      hiddenmodalput: !that.data.hiddenmodalput
    })
    wx.setStorageSync('PED', that.data.equ.id)  //将设备ID设置到缓存中
  },
  //取消按钮
  cancel: function () {
    var that = this;
    that.setData({ hiddenmodalput: !that.data.hiddenmodalput });
    console.log('用户点击取消')
  },
  //确认    
  confirm: function () {
    var that = this;
    that.setData({ hiddenmodalput: !that.data.hiddenmodalput });
    console.log('用户点击确认')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})